const BASE_URL = 'http://localhost:8080/api/v1'
/**
 * 
 * @param {Object} obj 
 */
const request = function ({ type, url, data, dataType, success }) {
    const isFormData = data instanceof FormData
    $.ajax({
        type,
        url: BASE_URL + url,
        data,
        dataType,
        processData: isFormData? false: true,
        contentType: isFormData? false: 'application/x-www-form-urlencoded',
        headers: {
            Authorization: window.localStorage.getItem('bignews_token')
        },
        success: function (response) {
            success(response)
        },
        error: function (jqXHR,textStatus) {
            if(jqXHR.status === 403){
                window.location.href = 'login.html'
            } else if (jqXHR.status === 400) {
                alert(jqXHR.responseJSON.msg)
            }
        }
    });
}